const db = require('../db/index');
const path = require('path');

exports.addArticle = (req, res) => {

  // 手动判断是否上传了文章封面
  if (!req.file || req.file.fieldname !== 'cover_img') {
    return res.cc('文章封面是必选参数！')
  }
  const articleInfo = {
    ...req.body,
    cover_img: path.join('/uploads', req.file.filename),
    pub_date: new Date(),
    author_id: req.user.id,
  }
// 发布文章
  const sql = 'insert into ev_articles set ?';
  db.query(sql, articleInfo, (err, result) => {
    if (err) return res.cc(err);
    if (result.affectedRows !== 1) {
      return res.cc('发布文章失败！')
    }
    res.cc('发布文章成功！', 0)
  })
}
// 分页获取文章列表
exports.getArticleList = (req, res) => {
  const { cate_id, state } = req.query;
  let page = req.query.page || 1;
  let pageSize = req.query.pageSize || 5;
  // sql语句的关联表查询
  const sql = `select e.id,e.title,e.pub_date,e.state,e.cate_id,e.author_id,c.name as cate_name from ev_articles as e left join ev_article_cate as c on e.cate_id=c.id where e.is_delete=0 limit ${pageSize * (page - 1)},${pageSize}`;
  const sql2 = `select e.id,e.title,e.pub_date,e.state,e.cate_id,e.author_id,c.name as cate_name from ev_articles as e left join ev_article_cate as c on e.cate_id=c.id where e.is_delete=0 and cate_id=? limit ${pageSize * (page - 1)},${pageSize}`;
  const sql3 = `select e.id,e.title,e.pub_date,e.state,e.cate_id,e.author_id,c.name as cate_name from ev_articles as e left join ev_article_cate as c on e.cate_id=c.id where e.is_delete=0 and state=? limit ${pageSize * (page - 1)},${pageSize}`;
  const sql4 = `select e.id,e.title,e.pub_date,e.state,e.cate_id,e.author_id,c.name as cate_name from ev_articles as e left join ev_article_cate as c on e.cate_id=c.id where e.is_delete=0 and cate_id=? and state=? limit ${pageSize * (page - 1)},${pageSize}`;
  const totalSql = 'select count(id) as total from ev_articles where is_delete=0';
  db.query(totalSql, (err, result) => {
    if (err) return res.cc(err);
    if (!cate_id && !state) {
      db.query(sql, (err, results) => {
        if (err) return res.cc(err);
        res.send({
          code: 0,
          message: '获取文章列表成功！',
          data: results,
          total: result[0].total,
        })
      })
    } else if (cate_id && !state) {
      db.query(sql2, cate_id, (err, results) => {
        if (err) return res.cc(err);
        res.send({
          code: 0,
          message: '获取文章列表成功！',
          data: results,
          total: result[0].total,
        })
      })
    } else if (!cate_id && state) {
      db.query(sql3, state, (err, results) => {
        if (err) return res.cc(err);
        res.send({
          code: 0,
          message: '获取文章列表成功！',
          data: results,
          total: result[0].total,
        })
      })
    } else {
      db.query(sql4, [cate_id, state], (err, results) => {
        if (err) return res.cc(err);
        res.send({
          code: 0,
          message: '获取文章列表成功！',
          data: results,
          total: result[0].total,
        })
      })
    }
  })

}
// 根据id删除
exports.deleteArticle = (req, res) => {
  const sql = 'update ev_articles set is_delete=1 where id=?';
  db.query(sql, req.params.id, (err, result) => {
    if (err) return res.cc(err);
    if (result.affectedRows !== 1) {
      return res.cc('删除文章失败！')
    }
    res.cc('删除文章成功！', 0)
  })
}
// 根据id获取
exports.getArticle = (req, res) => {
  const sql = 'select * from ev_articles where id=?';
  db.query(sql, req.params.id, (err, results) => {
    if (err) return res.cc(err);
    res.send({
      code: 0,
      message: '获取文章成功！',
      data: results[0],
    })
  })
}
// 根据id更新
exports.updateArticle = (req, res) => {
  const sql = 'update ev_articles set ? where id=?';
  db.query(sql, [req.body, req.body.id], (err, result) => {
    if (err) return res.cc(err);
    if (result.affectedRows !== 1) {
      return res.cc('更新文章失败！')
    }
    res.cc('更新文章成功！', 0)
  })
}
